package com.xubeizhi.controller.system;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xubeizhi.annotation.OperationLogger;
import com.xubeizhi.common.ResponseResult;
import com.xubeizhi.entity.Job;
import com.xubeizhi.enums.TaskException;
import com.xubeizhi.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定时任务调度表  前端控制器
 * @author mi
 * @create 2023/3/31 10:16
 */
@RestController
@RequestMapping("/system/job")
@RequiredArgsConstructor
@Api(tags = "定时任务管理")
public class JobController {

    private final JobService jobService;


    @GetMapping(value = "/list")
    @SaCheckLogin
    @ApiOperation(value = "任务列表",httpMethod = "GET",response = ResponseResult.class,notes = "任务列表")
    public ResponseResult list(String jobName,String jobGroup,String status){
        return jobService.listJob(jobName,jobGroup,status);
    }

    @GetMapping(value = "/info")
    @SaCheckPermission("/system/job/info")
    @ApiOperation(value = "任务详情",httpMethod = "GET",response = ResponseResult.class,notes = "任务详情")
    @OperationLogger(value = "任务详情")
    public ResponseResult getJobById(Long jobId){
        return jobService.getJobById(jobId);
    }


    @PostMapping(value = "/add")
    @SaCheckPermission("/system/job/add")
    @ApiOperation(value = "新增任务",httpMethod = "POST",response = ResponseResult.class,notes = "新增任务")
    @OperationLogger(value = "新增任务")
    public ResponseResult insert(@RequestBody Job job) throws SchedulerException, TaskException {
        return jobService.insertJob(job);
    }

    @PostMapping(value = "/update")
    @SaCheckPermission("/system/job/update")
    @ApiOperation(value = "修改任务",httpMethod = "POST",response = ResponseResult.class,notes = "修改任务")
    @OperationLogger(value = "修改任务")
    public ResponseResult update(@RequestBody Job job) throws SchedulerException, TaskException {
        return jobService.updateJob(job);
    }


    @DeleteMapping(value = "/delete")
    @SaCheckPermission("/system/job/delete")
    @ApiOperation(value = "删除任务",httpMethod = "DELETE",response = ResponseResult.class,notes = "删除任务")
    @OperationLogger(value = "删除任务")
    public ResponseResult deleteJob(Long jobId) throws SchedulerException {
        return jobService.deleteJob(jobId);
    }

    @DeleteMapping(value = "/deleteBatch")
    @SaCheckPermission("/system/job/deleteBatch")
    @ApiOperation(value = "批量删除任务",httpMethod = "DELETE",response = ResponseResult.class,notes = "批量删除任务")
    @OperationLogger(value = "批量删除任务")
    public ResponseResult deleteBatch(@RequestBody List<Long> ids){
        return jobService.deleteBatch(ids);
    }

    @PostMapping("/run")
    @SaCheckPermission("/system/job/run")
    @ApiOperation(value = "立即执行",httpMethod = "POST",response = ResponseResult.class,notes = "立即执行")
    @OperationLogger(value = "立即执行")
    public ResponseResult run(@RequestBody Job job){
        return jobService.run(job);
    }

    @PostMapping(value = "/change")
    @SaCheckPermission("/system/job/change")
    @ApiOperation(value = "修改状态",httpMethod = "POST",response = ResponseResult.class,notes = "修改状态")
    @OperationLogger(value = "修改状态")
    public ResponseResult changeStatus(@RequestBody Job job) throws SchedulerException {
        return jobService.changeStatus(job);
    }
}
